package yxy.game.sdk.jhh5;

import com.alibaba.fastjson.JSONObject;

import yxy.apple.http.server.business.BusinessType;
import yxy.apple.logger.Logger;
import yxy.game.bean.LoginOtherParm;
import yxy.game.login.Error;
import yxy.game.login.LoginModule;
import yxy.game.util.HttpUtil;

@BusinessType(code = "LOGIN")
public class Login extends Sdk_jhh5 {

	static final Logger logger = Logger.create(Login.class);
	
	@Override
	public JSONObject handle(JSONObject request) throws Exception {
		
		System.out.println("-------------SDK_JHH5--------------\n" + request.toString());
		JSONObject result = new JSONObject();
		JSONObject infoobj = request.getJSONObject("datajson");

		if(!infoobj.containsKey("appCode")) {
			return result(result, Error.Err_渠道代码错误);
		}
		String appCode = infoobj.getString("appCode");
		
		if(!infoobj.containsKey("uid")) {
			return result(result, Error.Err_渠道代码错误);
		}
		long uid = infoobj.getLongValue("uid");
		
		if(!infoobj.containsKey("token")) {
			return result(result, Error.Err_渠道代码错误);
		}
		String token = infoobj.getString("token");
		
		JSONObject postjson = new JSONObject();
		postjson.put("appCode", appCode);
		postjson.put("uid", uid);
		postjson.put("token", token);
		
		System.out.println("---------SDK_JHH5-----------postjson--------\n" + postjson);
		JSONObject retjson = HttpUtil.response(chexklogin.toString(), 30000, postjson);	
		System.out.println("---------SDK_JHH5-----------ret--------\n" + retjson);
		
		if(retjson == null || retjson.size() == 0) {
			return result(result, Error.Err_渠道代码错误);
		}
		
//		返回参数说明
//		参数名	类型	说明
//		statusCode	String	200表示校验成功，其他失败
		if(!retjson.containsKey("statusCode")) {
			return result(result, Error.Err_渠道代码错误);
		}
		
		int statusCode = retjson.getIntValue("statusCode");
		if(statusCode != success) {
			return result(result, Error.Err_渠道代码错误);
		}
		
		String username = String.valueOf(uid);
		String password = username;
		String channel = name();
		
		String ip = request.getString("ip");
		String subchannel = channel;
		int clusterId = request.getInteger("clusterId");
		LoginOtherParm loginOtherParm = new LoginOtherParm();
		loginOtherParm.setClusterId(clusterId);
		return new LoginModule().login(username, password, channel, subchannel, ip, loginOtherParm);
	}

	public JSONObject result(JSONObject result, Error errcode) {
		result.put("result", errcode.number());
		return result;
	}
	
	
//	public static void main(String[] args) {
////		appCode: "0E8C718D0B815FE3"
////		channelId: ""
////		child: "1"
////		token: "b34bc24e2bafa21bd7fa8bb9f1fd7d8d"
////		uid: "31341278"
////		v: "78217"
//		JSONObject postjson = new JSONObject();
//		postjson.put("appCode", "0E8C718D0B815FE3");
//		postjson.put("uid", "31341278");
//		postjson.put("token", "b34bc24e2bafa21bd7fa8bb9f1fd7d8d");
//		
//		System.out.println("---------SDK_JHH5-----------postjson--------\n" + postjson);
//		JSONObject retjson = null;
//		try {
//			retjson = HttpUtil.response(chexklogin.toString(), 30000, postjson);
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}	
//		System.out.println("---------SDK_JHH5-----------ret--------\n" + retjson);
//	}
}
